using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesToolsEntertainmentForCust : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected EntertainmentHeader ResultObj = null;
    protected readonly int TotalColumns = 6;
    protected readonly int DefaultRowNumber = 5;
    protected bool ReadOnly = false;
    protected int CurRow = 1;

    #region IsNew

    protected bool IsNew
    {
        get
        {
            return (bool)ViewState["IsNew"];
        }

        set
        {
            ViewState["IsNew"] = value;
        }
    }

    #endregion

    #region EntertainmentID

    protected string EntertainmentID
    {
        get
        {
            return ViewState["EntertainmentID"].ToString();
        }

        set
        {
            ViewState["EntertainmentID"] = value;
        }
    }

    #endregion

    #region ClaimItemId
    protected string ClaimItemId
    {
        get
        {
            return ViewState["ClaimItemId"].ToString();
        }

        set
        {
            ViewState["ClaimItemId"] = value;
        }
    }
    #endregion

    #region ClaimInternalID
    protected string ClaimInternalID
    {
        get
        {
            return ViewState["ClaimInternalID"].ToString();
        }

        set
        {
            ViewState["ClaimInternalID"] = value;
        }
    }
    #endregion

    #region Components for Claim Detail Table Grid

    protected int ClaimDetailRowBuiltCounter = 0;   // everytime will be 0

    #region ActualNumberOfRow
    protected int ActualNumberOfRow
    {
        get
        {
            return (int)ViewState["ActualNumberOfRow"];
        }

        set
        {
            ViewState["ActualNumberOfRow"] = value;
        }
    }
    #endregion

    #region ClaimDetailIDList
    protected ArrayList ClaimDetailIDList
    {
        get
        {
            return (ArrayList)ViewState["ClaimDetailIDList"];
        }

        set
        {
            ViewState["ClaimDetailIDList"] = value;
        }
    }
    #endregion

    #endregion

    #region Common Variables for Language Translation

    #region ValidationText
    protected string ValidationText
    {
        get
        {
            return ViewState["ValidationText"].ToString();
        }

        set
        {
            ViewState["ValidationText"] = value;
        }
    }
    #endregion


    #endregion

    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.BuiltTable();

        if (!IsPostBack)
        {
            #region Claim ID and Claim detail id

            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36 &&
                Request.QueryString["did"] != null && Request.QueryString["did"].Length == 36)
            {
                this.ClaimInternalID = Request.QueryString["id"];
                this.ClaimItemId = Request.QueryString["did"];
            }
            else
            {
                // redirect 
            }

            #endregion

            this.IsNew = false;

            if (Request.QueryString["NoEdit"] != null && Request.QueryString["NoEdit"].CompareTo("1") == 0)
            {
                this.ReadOnly = true;
            }

            this.PageComponentSetup();

            this.LoadData();

            this.ClaimDetailIDList = new ArrayList();

            if (this.ResultObj != null)
            {
                int RowNeeded = (this.ResultObj.Details.Count > this.DefaultRowNumber) ? 0 : (this.DefaultRowNumber - this.ResultObj.Details.Count);
                int TotalRowToBuilt = RowNeeded + this.ResultObj.Details.Count;

                for (int x = 0; x < TotalRowToBuilt; x++)
                {
                    if (x < this.ResultObj.Details.Count)
                    {
                        this.BuildRow(this.ResultObj.Details[x].InternalID);
                    }
                    else
                    {
                        this.BuildRow();
                    }
                }

                this.FillData();
                this.LoadIntoDetailGrid();
            }
            else
            {
                for (int x = 0; x < this.DefaultRowNumber; x++)
                {
                    this.BuildRow();
                }

                // if this is a new entertainment, pre-load with claimant
                if (this.IsNew)
                {
                    #region 1. Attendee

                    TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[1].Cells[1].Controls[0];
                    TextBox_Attendee.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.DisplayName;

                    #endregion

                    #region 3. Company

                    TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[1].Cells[3].Controls[0];
                    TextBox_Company.Text = this.CurrentPrivateSession.CurrentLoggedOnUser.FromCompany.CompanyName;

                    #endregion
                }
            }
        }
        else
        {
            // this is a post back
            for (int x = 0; x < this.ClaimDetailIDList.Count; x++)
            {
                this.BuildRow(this.ClaimDetailIDList[x].ToString());
            }
        }
    }
    #endregion

    #region Button_AddMoreRow_Click
    protected void Button_AddMoreRow_Click(object sender, EventArgs e)
    {
        for (int x = 0; x < this.DefaultRowNumber; x++)
        {
            this.BuildRow();
        }
    }
    #endregion

    #region Button_Submit_Click
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            this.HiddenField_TotalParticipants.Value = this.ResultObj.Details.Count.ToString();
            this.ShowMessage("Data saved!");
        }
    }
    #endregion

    #region Button_Done_Click
    protected void Button_Done_Click(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            this.HiddenField_TotalParticipants.Value = this.ResultObj.Details.Count.ToString();
            this.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "Close", "<script language='javascript'>parent.ModalWindow.hide();</script>");
        }
    }
    #endregion

    #region Button_Remove_Click
    protected void Button_Remove_Click(object sender, EventArgs e)
    {
        int x = 1;

        while (x < (this.Table_ClaimDetailGrid.Rows.Count))
        {
            if (((CheckBox)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[0]).Checked)
            {
                for (int y = 0; y < this.ClaimDetailIDList.Count; y++)
                {
                    if (this.ClaimDetailIDList[y].ToString().CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                    {
                        this.ClaimDetailIDList.RemoveAt(y);
                    }
                }

                this.Table_ClaimDetailGrid.Rows.RemoveAt(x);
            }
            else
            {
                x++;
            }
        }

    }
    #endregion

    #region Button_ClearAll_Click
    protected void Button_ClearAll_Click(object sender, EventArgs e)
    {
        this.Table_ClaimDetailGrid.Rows.Clear();
        this.BuiltTable();
        this.TextBox_TotalCost.Text = "0";

    }
    #endregion

    #region Button_Copy_Click
    protected void Button_Copy_Click(object sender, EventArgs e)
    {
        this.DataCollect();

        if (this.DataSave())
        {
            Session[GonzalesCommonNamingConvention.CurrentClipBoardSession] = this.EntertainmentID;
            this.ShowMessage("Name list copied.");
        }

    }
    #endregion

    #region Button_PasteNameList_Click
    protected void Button_PasteNameList_Click(object sender, EventArgs e)
    {
        if (Session[GonzalesCommonNamingConvention.CurrentClipBoardSession] != null &&
            Session[GonzalesCommonNamingConvention.CurrentClipBoardSession].ToString().Length == 36)
        {
            using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
            {
                EntertainmentDetails CurNameListDetail = CurClaimManager.GetEntertainmentDetails(Session[GonzalesCommonNamingConvention.CurrentClipBoardSession].ToString());

                if (CurNameListDetail != null)
                {
                    for (int x = (this.Table_ClaimDetailGrid.Rows.Count - 1); x > 0; x--)
                    {
                        HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                        if (HiddenField_IsEmpty.Value.CompareTo("1") == 0)
                        {
                            // remove from detail id
                            for (int y = 0; y < this.ClaimDetailIDList.Count; y++)
                            {
                                if (this.ClaimDetailIDList[y].ToString().CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                                {
                                    this.ClaimDetailIDList.RemoveAt(y);
                                    break;
                                }
                            }

                            // remove the empty row
                            this.Table_ClaimDetailGrid.Rows.RemoveAt(x);
                        }
                        else
                        {
                            break;  // break anytime if meet 1 row is not empty
                        }
                    }

                    // remove the emoty row
                    //this.Table_ClaimDetailGrid.Rows.RemoveAt(LastEmptyRowIndex);

                    if (CurNameListDetail.Count > 0)
                    {
                        for (int x = 0; x < CurNameListDetail.Count; x++)
                        {
                            this.BuildRow();

                            // fill data in latest row
                            TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[2].Controls[0];
                            TextBox_Attendee.Text = CurNameListDetail[x].Attendess;

                            TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[3].Controls[0];
                            TextBox_Title.Text = CurNameListDetail[x].Title;

                            TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[4].Controls[0];
                            TextBox_Company.Text = CurNameListDetail[x].Company;
                        }

                        // recalculate cost per head
                        int TotalEmptyRow = 0;
                        for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
                        {
                            TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[2].Controls[0];
                            TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[3].Controls[0];
                            TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[4].Controls[0];
                            TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[(this.Table_ClaimDetailGrid.Rows.Count - 1)].Cells[5].Controls[0];

                            if (TextBox_Attendee.Text.Trim().Length == 0 && TextBox_Title.Text.Trim().Length == 0 &&
                                TextBox_Company.Text.Trim().Length == 0 && TextBox_CostPerHead.Text.CompareTo("0") == 9)
                            {
                                TotalEmptyRow++;
                                HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                                HiddenField_IsEmpty.Value = "1";    // marked empty
                            }
                        }

                        if (decimal.Parse(this.TextBox_TotalCost.Text) > 0)
                        {
                            try
                            {
                                decimal CostPerHead = decimal.Parse(this.TextBox_TotalCost.Text) / (this.Table_ClaimDetailGrid.Rows.Count - 1 - TotalEmptyRow);

                                for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
                                {
                                    HiddenField HiddenField_IsEmpty = (HiddenField)this.Table_ClaimDetailGrid.Rows[x].Cells[0].Controls[1];
                                    if (HiddenField_IsEmpty.Value.CompareTo("1") != 0)
                                    {
                                        TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
                                        TextBox_CostPerHead.Text = CostPerHead.ToString();
                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                string LogID = SwissArmy.UniqueID();
                                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.Button_PasteNameList_Click] : Exception : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                            }

                        }


                    }
                }
                else
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.Button_PasteNameList_Click] : Failed at CurClaimManager.GetEntertainmentDetails : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                }

            }

        }

    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.Button_Done.Attributes["onclick"] = "ReCalCostPerHead();return Validate();";
        this.Button_Remove.Attributes["onclick"] = "return confirm('Are you sure? Please remember to save your entertainment list.');";
        this.Button_Calculate.Attributes["onclick"] = "ReCalCostPerHead(); return false;";
        this.Button_PasteNameList.Attributes["onclick"] = "ReCalCostPerHead();";
        this.Button_Submit.Attributes["onclick"] = "ReCalCostPerHead();return Validate();";
        this.Button_Cancel.Attributes["onclick"] = "if (PassParameter()) {parent.ModalWindow.hide();}return false;"; 

        this.TextBox_TotalCost.Text = "0";

        if (this.ReadOnly)
        {
            this.Button_ClearAll.Visible = this.Button_Done.Visible = this.Button_AddMoreRow.Visible = this.Button_Remove.Visible = this.Button_Submit.Visible = this.Button_Calculate.Visible = this.Button_Copy.Visible = this.Button_PasteNameList.Visible = false;
            this.TextBox_Purpose.Attributes["onfocus"] = "this.blur();";
            this.CheckBox_IsGovOfficial.Enabled = false;
        }

    }
    #endregion

    #region BuiltTable

    protected void BuiltTable()
    {
        // add all the table header
        TableRow HeaderRow = new TableRow();
        int CellCounter = 1;

        for (int x = 0; x < this.TotalColumns; x++)
        {
            HeaderRow.Cells.Add(new TableCell());
            HeaderRow.Cells[x].CssClass = "GridViewHeader";
        }

        HeaderRow.Cells[CellCounter++].Text = "No.";
        HeaderRow.Cells[CellCounter++].Text = "Attendees";
        HeaderRow.Cells[CellCounter++].Text = "Titles";
        HeaderRow.Cells[CellCounter++].Text = "Affiliations/Companys";
        HeaderRow.Cells[CellCounter++].Text = "Cost per head";

        this.Table_ClaimDetailGrid.Rows.Add(HeaderRow);
    }

    #endregion

    #region BuiltRow

    protected void BuildRow()
    {
        this.BuildRow(SwissArmy.UniqueID());
    }

    protected void BuildRow(string RowID)
    {
        TableRow NewTableRow = new TableRow();
        int CellCounter = 0;

        NewTableRow.ID = RowID;

        if (!this.ClaimDetailIDList.Contains(RowID))
        {
            this.ClaimDetailIDList.Add(NewTableRow.ID); // capture the row id using in the system
        }

        for (int x = 0; x < this.TotalColumns; x++)
        {
            // set default setting to all the row
            NewTableRow.Cells.Add(new TableCell());
            NewTableRow.Cells[x].CssClass = "GridViewItem";
            NewTableRow.Cells[x].HorizontalAlign = HorizontalAlign.Center;
        }

        #region 0. Delete Indicator

        CheckBox CheckBoxDelete = new CheckBox();
        CheckBoxDelete.ID = NewTableRow.ID + "_Delete";
        CheckBoxDelete.Visible = !this.ReadOnly;

        HiddenField HiddenFieldIsEmpty = new HiddenField();
        HiddenFieldIsEmpty.ID = NewTableRow.ID + "_IsEmpty";

        NewTableRow.Cells[CellCounter].Controls.Add(CheckBoxDelete);
        NewTableRow.Cells[CellCounter].Controls.Add(HiddenFieldIsEmpty);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(20);

        #endregion

        #region Extra Column to indicate the No.
        Label LabelNo = new Label();
        LabelNo.ID = RowID + "_No";
        LabelNo.Text = CurRow.ToString();
        CurRow++;

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(LabelNo);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(20);
        #endregion

        #region 1. Attendees

        TextBox TextBox_Attendees = new TextBox();
        TextBox_Attendees.ID = RowID + "TextBox_Attendees";
        TextBox_Attendees.CssClass = "DetailGridTextBox";
        TextBox_Attendees.Width = new Unit(95, UnitType.Percentage);

        if (this.ReadOnly)
        {
            TextBox_Attendees.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Attendees);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        #region 2. Title

        TextBox TextBox_Title = new TextBox();
        TextBox_Title.ID = RowID + "TextBox_Title";
        TextBox_Title.CssClass = "DetailGridTextBox";
        TextBox_Title.Width = new Unit(95, UnitType.Percentage);

        if (this.ReadOnly)
        {
            TextBox_Title.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Title);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        #region 3. Company

        TextBox TextBox_Company = new TextBox();
        TextBox_Company.ID = RowID + "TextBox_Company";
        TextBox_Company.CssClass = "DetailGridTextBox";
        TextBox_Company.Width = new Unit(95, UnitType.Percentage);

        if (this.ReadOnly)
        {
            TextBox_Company.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_Company);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        #region 4. Cost Per Head

        TextBox TextBox_CostPerHead = new TextBox();
        TextBox_CostPerHead.ID = RowID + "TextBox_CostPerHead";
        TextBox_CostPerHead.CssClass = "DetailGridTextBox";
        TextBox_CostPerHead.Style.Add(HtmlTextWriterStyle.TextAlign, "right");
        TextBox_CostPerHead.Width = new Unit(95, UnitType.Percentage);

        if (this.ReadOnly)
        {
            TextBox_CostPerHead.Attributes["onfocus"] = "this.blur();";
        }

        CellCounter++;
        NewTableRow.Cells[CellCounter].Controls.Add(TextBox_CostPerHead);
        NewTableRow.Cells[CellCounter].Width = Unit.Pixel(100);

        #endregion

        // done
        this.Table_ClaimDetailGrid.Rows.Add(NewTableRow);
        this.ClaimDetailRowBuiltCounter++;
    }

    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ResultObj = CurClaimManager.GetEntertainmentHeader(this.ClaimInternalID, this.ClaimItemId);

            if (ResultObj != null)
            {
                this.EntertainmentID = ResultObj.InternalID;

                // load the items
                this.ResultObj.Details = CurClaimManager.GetEntertainmentDetails(ResultObj.InternalID);
                if (this.ResultObj.Details == null && CurClaimManager.ErrorMessage.Length > 0)
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentDetails('" + ResultObj.InternalID + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage("Error! " + LogID);
                }
            }
            else if (CurClaimManager.ErrorMessage.Length > 0)
            {
                // log reasons
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentHeader('" + this.ClaimInternalID + "', '" + this.ClaimItemId + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage("Error! " + LogID);
            }
            else
            {
                // this is a new entertainment list
                this.EntertainmentID = SwissArmy.UniqueID();
                this.IsNew = true;
            }

        }

    }
    #endregion

    #region FillData

    protected void FillData()
    {
        if (this.ResultObj != null)
        {
            this.TextBox_Purpose.Text = this.ResultObj.Purpose;
            this.CheckBox_IsGovOfficial.Checked = this.ResultObj.GovOfficial;
            this.TextBox_TotalCost.Text = this.ResultObj.TotalCost.ToString();
        }
    }

    #endregion

    #region LoadIntoDetailGrid
    protected void LoadIntoDetailGrid()
    {
        int TotalCurrentRow = this.Table_ClaimDetailGrid.Rows.Count - 1;
        int ExtraRowNeeded = (this.ResultObj.Details.Count > TotalCurrentRow) ? (this.ResultObj.Details.Count - TotalCurrentRow) : 0;

        for (int x = 0; x < ExtraRowNeeded; x++)
        {
            // built extra row to cater current claim detail
            this.BuildRow();
        }

        // load into grid now
        for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
        {
            for (int y = 0; y < this.ResultObj.Details.Count; y++)
            {
                if (this.ResultObj.Details[y].InternalID.CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                {
                    #region 1. Attendee

                    TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[2].Controls[0];
                    TextBox_Attendee.Text = this.ResultObj.Details[y].Attendess;

                    #endregion

                    #region 2. Title

                    TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0];
                    TextBox_Title.Text = this.ResultObj.Details[y].Title;

                    #endregion

                    #region 3. Company

                    TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[4].Controls[0];
                    TextBox_Company.Text = this.ResultObj.Details[y].Company;

                    #endregion

                    #region 4. Cost Per Head

                    TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
                    TextBox_CostPerHead.Text = this.ResultObj.Details[y].CostPerHead.ToString();

                    #endregion

                    break;
                }
            }

            // cost per head
            TextBox TextBox_CurCostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];
            if (TextBox_CurCostPerHead.Text.Length == 0)
            {
                TextBox_CurCostPerHead.Text = "0";
            }
        }

        // remove extra row if this is a read only
        if (this.ReadOnly)
        {
            if ((this.Table_ClaimDetailGrid.Rows.Count - 1) > this.ResultObj.Details.Count)
            {
                int RowToDelete = (this.Table_ClaimDetailGrid.Rows.Count - 1) - this.ResultObj.Details.Count;
                int Counter = 0;

                for (int x = (this.Table_ClaimDetailGrid.Rows.Count - 1); x > 1; x--)
                {
                    Counter++;
                    this.Table_ClaimDetailGrid.Rows[x].Visible = false;

                    if (Counter == RowToDelete)
                    {
                        break;
                    }

                }
            }
        }

    }

    #endregion

    #region DataSave
    protected bool DataSave()
    {
        bool smooth = true;

        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            if (!(smooth = CurClaimManager.SaveEntertainmentRemarks(this.ResultObj, this.ClaimInternalID, this.ClaimItemId)))
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment.SaveData] : Failed at CurClaimManager.SaveEntertainmentRemarks() : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage("Error! " + LogID);
            }
        }

        return smooth;
    }
    #endregion

    #region DataCollect

    protected void DataCollect()
    {
        this.LoadData(); // load data first

        if (!this.Table_MessageBox.Visible)
        {
            if (this.ResultObj == null)
            {
                // entertainment remarks is optional, hence if dun hav... juz create 1
                // use the one created during begining
                this.ResultObj = new EntertainmentHeader(this.EntertainmentID);
            }

            this.ResultObj.Purpose = this.TextBox_Purpose.Text;
            this.ResultObj.GovOfficial = this.CheckBox_IsGovOfficial.Checked;
            this.ResultObj.Details = new EntertainmentDetails();
            this.ResultObj.TotalCost = decimal.Parse(this.TextBox_TotalCost.Text);

            int OrderCounter = 0;

            for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
            {
                TextBox TextBox_Attendee = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[2].Controls[0];
                TextBox TextBox_Title = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0];
                TextBox TextBox_Company = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[4].Controls[0];
                TextBox TextBox_CostPerHead = (TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[5].Controls[0];

                if (TextBox_Attendee.Text.Length > 0 && TextBox_Title.Text.Length > 0 && TextBox_Company.Text.Length > 0)
                {
                    EntertainmentDetail CurrentDetail = new EntertainmentDetail(this.Table_ClaimDetailGrid.Rows[x].ID);
                    CurrentDetail.Attendess = TextBox_Attendee.Text;
                    CurrentDetail.Title = TextBox_Title.Text;
                    CurrentDetail.Company = TextBox_Company.Text;
                    if (TextBox_CostPerHead.Text.Length > 0)
                        CurrentDetail.CostPerHead = decimal.Parse(TextBox_CostPerHead.Text);
                    else
                        CurrentDetail.CostPerHead = 0;
                    CurrentDetail.Order = OrderCounter++;

                    this.ResultObj.Details.Add(CurrentDetail);
                }
            }
        }
    }

    #endregion

    #endregion

}
